package com.foreverwin.jzymes.production.sfc.controller;

import com.foreverwin.jzymes.common.constant.Constants;
import com.foreverwin.jzymes.common.service.*;
import com.foreverwin.jzymes.common.util.BOHandleUtil;
import com.foreverwin.jzymes.common.util.CommonMethods;
import com.foreverwin.jzymes.framework.base.AjaxResult;
import com.foreverwin.jzymes.framework.base.BaseController;
import com.foreverwin.jzymes.framework.exception.BusinessException;
import com.foreverwin.jzymes.framework.util.I18nUtil;
import com.foreverwin.jzymes.production.sfc.domain.SFC;
import com.foreverwin.jzymes.production.sfc.enums.ProcedureEnum;
import com.foreverwin.jzymes.production.sfc.service.ISFCService;
import com.foreverwin.jzymes.production.sfc.service.ISFCServiceAdvice;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/production/sfc")
public class SFCController extends BaseController {

    @Autowired
    ISFCServiceAdvice sfcServiceAdvice;
    @Autowired
    ISFCService sfcService;
    @Autowired
    ICustomFieldsAdviceService customFieldsAdviceService;
    @Autowired
    ISiteAdviceService siteAdviceService;
    @Autowired
    IFieldGroupAdviceService fieldGroupAdviceService;
    @Autowired
    ICommonService commonService;
    @Autowired
    IItemGroupAdviceService itemGroupAdviceService;

    @RequestMapping("/item")
    public AjaxResult findItemList( @RequestParam String procedure ){
        String site = CommonMethods.getSite();
        Assert.hasText( site, I18nUtil.getI18nText( "common.siteNotBlank" ));

        String itemGroup = itemGroupAdviceService.findItemGroupBySite( site );

        List<Map<String,Object>> itemTreeList = new ArrayList<>();
        if( ProcedureEnum.CNC == ProcedureEnum.valueOf( procedure ) ){
            commonService.findItemTree( site, itemGroup, itemTreeList, false, "M" );
        }else{
            commonService.findItemTree( site, itemGroup, itemTreeList, false, "B" );
        }

        List<Map<String,Object>> rtnItemList = new ArrayList<>();
        for( Map<String,Object> itemMap : itemTreeList ){
            String routerBo = null;
            String router = null;
            String routerVersion = null;
            if( ProcedureEnum.CNC == ProcedureEnum.valueOf( procedure ) ){
                routerBo = (String) itemMap.get( "ROUTER_BO" );
                router = BOHandleUtil.splitRouterHandle( routerBo )[1];
                routerVersion = BOHandleUtil.splitRouterHandle( routerBo )[3];
            }

            Map<String,Object> rtnItemMap = new HashMap<>();
            rtnItemMap.put( "machineType", itemGroup );
            rtnItemMap.put( "itemBo", itemMap.get( "HANDLE" ) );
            rtnItemMap.put( "item", itemMap.get( "ITEM" ) );
            rtnItemMap.put( "revision", itemMap.get( "REVISION" ) );
            rtnItemMap.put( "description", customFieldsAdviceService.existCustomValue((String) itemMap.get( "HANDLE" ), Constants.ITEM_CUSTOM_FIELD_ITEM_LABEL ) );
            rtnItemMap.put( "currentVersion", itemMap.get( "CURRENT_REVISION" ) );
            rtnItemMap.put( "unit", itemMap.get( "UNIT_OF_MEASURE" ) );
            rtnItemMap.put( "routerBo", routerBo );
            rtnItemMap.put( "router", router  );
            rtnItemMap.put( "routerVersion", routerVersion );
            rtnItemMap.put( "planQty", customFieldsAdviceService.findCustomValue( (String) itemMap.get( "HANDLE" ), Constants.ITEM_CUSTOM_FIELD_PLAN_QTY ) );
            rtnItemList.add( rtnItemMap );
        }
        return AjaxResult.returnSuccess().put( "value", rtnItemList );
    }

    /**
     * 创建工令
     *
     * @param sfc
     * @return
     */
    @RequestMapping("/submit")
    public AjaxResult submit( @RequestBody SFC sfc ){
        sfc.setSite( CommonMethods.getSite() );
        try{
            if( StringUtils.isBlank( sfc.getSfc() ) ){
                sfcServiceAdvice.createSFC( sfc );
            }else {
                sfcServiceAdvice.updateSFC( sfc );
            }
            return AjaxResult.returnSuccess().put( "value", sfc );
        }catch ( BusinessException e ) {
            return AjaxResult.returnError(e.getMessage());
        }
    }

    /**
     * 检索工令
     *
     * @return
     */
    @RequestMapping("/retrieve")
    public AjaxResult retrieve( @RequestParam String sfc  ){
        String site = CommonMethods.getSite();
        /**站点不能为空*/
        siteAdviceService.existSite( site );
        /**工令号不能为空*/
        Assert.hasText( site, I18nUtil.getI18nText("common.sfcNotBlank") );

        Map<String,Object> sfcMap = new HashMap<>();
        String handle = BOHandleUtil.buildSFCHandle( site, sfc );
        SFC originalSFC = sfcService.selectSFCById( handle );
        /**工令号对应的工令是否存在*/
        Assert.notNull( originalSFC, I18nUtil.getI18nText( "production.sfc.sfcNotExist", new String[]{ sfc } ) );

        String routerBo = null;
        String router = null;
        String routerVersion = null;
        if( originalSFC.getProcedureBo().equals( BOHandleUtil.buildFieldGroupHandle( site, ProcedureEnum.CNC.name() ) ) ){
            routerBo = originalSFC.getRouterBo();
            router = BOHandleUtil.splitRouterHandle( routerBo )[1];
            routerVersion = BOHandleUtil.splitRouterHandle( routerBo )[3];
        }

        sfcMap.put( "sfc", originalSFC.getSfc() );
        sfcMap.put( "itemBo", originalSFC.getItemBo() );
        sfcMap.put( "item", BOHandleUtil.splitItemHandle( originalSFC.getItemBo() )[1] );
        sfcMap.put( "revision", BOHandleUtil.splitItemHandle( originalSFC.getItemBo() )[2] );
        sfcMap.put( "description", customFieldsAdviceService.existCustomValue( originalSFC.getItemBo(), Constants.ITEM_CUSTOM_FIELD_ITEM_LABEL ) );
        sfcMap.put( "routerBo", routerBo );
        sfcMap.put( "router", router );
        sfcMap.put( "routerVersion", routerVersion );
        sfcMap.put( "machineType", originalSFC.getMachineType() );
        sfcMap.put( "procedureBo", fieldGroupAdviceService.getFieldName( originalSFC.getProcedureBo( ) ) );
        sfcMap.put( "planQty", originalSFC.getPlanQty() );
        sfcMap.put( "sfcQty", originalSFC.getSfcQty() );
        sfcMap.put( "planStartTime", originalSFC.getPlanStartTime() );

        return AjaxResult.returnSuccess().put( "value", sfcMap );
    }

    /**
     * 删除工令
     *
     * @param sfc
     * @return
     */
    @RequestMapping("/delete")
    public AjaxResult deleteSfc( @RequestBody SFC sfc ){
        sfc.setSite( CommonMethods.getSite() );
        try{
            sfcServiceAdvice.deleteSFC( sfc );
            return AjaxResult.returnSuccess().put( "value", sfc );
        }catch ( BusinessException e ) {
            return AjaxResult.returnError(e.getMessage());
        }
    }


}
